package com.zwh.rabbitmq.basic.exchange;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 显示声明直连交换机，routing key值相等匹配
 * @author monkey
 * @date 2020/11/3 10:38
 **/
@EnableRabbit
@Configuration
public class DirectExchangeConfig {
    /**
     * 声明直连交换机名称， 秒杀直连交换机
     */
    public static final String TEST_DIRECT_EXCHANGE = "testDirectExchange";

    /**
     * 直连队列1
     */
    public static final String DIRECT_QUEUE1 = "directQueue1";
    /**
     * 直连队列1
     */
    public static final String DIRECT_QUEUE2 = "directQueue2";

    /**
     * 声明直连交换机的bean， 该bean名称为：testDirectExchange
     * AbstractExchange(String name, boolean durable, boolean autoDelete)
     * this(name, true, false);
     * 默认情况下，交换机是持久化，且不会自动删除
     * @return
     */
    @Bean
    public DirectExchange testDirectExchange(){
        // 直连消息队列参数， 指定备用交换机，testDirectExchange交换机不能用时，会将消息发送到testDirectExchange_alternate交换机上
        Map<String, Object> otherArguments = new HashMap<>(1);
        otherArguments.put("alternate-exchange", "testDirectExchange_alternate");
        return new DirectExchange("testDirectExchange", true, false,  otherArguments);
    }

    /**
     * 声明直连队列的bean， 该bean名称为：testDirectQueue1
     * @return
     */
    @Bean
    public Queue testDirectQueue1() {
        return new Queue(DIRECT_QUEUE1);
    }

    /**
     * 将声明的队列绑定到直连交换机， 绑定的路由key routeKey为队列名称
     * @return
     */
    @Bean
    public Binding testDirectQueue1Binding(){
        return BindingBuilder.bind(testDirectQueue1()).to(testDirectExchange()).with(DIRECT_QUEUE1);
    }
}
